﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Hospital_Payment : System.Web.UI.Page, HZ.MBSM.BLL.TraDes
{
    // public Int32 PayMentProjectId;  2007/08/22 马
    Int64 PayMentHospitalId;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {

            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                // PayMentProjectId = SysFun.ToInt(Request.QueryString["ProjectId"]);    2007/08/22 马
                PayMentHospitalId = SysFun.ToLong(_Account.AccountId);
                if (Request.Form["del"] == "删除结算单" && Request.Form["delcheck"] != null)
                {
                    string StrSql, PaymentIdString,updatesql;
                    Db db = new Db();
                    PaymentIdString = Request.Form["delcheck"];
                    StrSql = "Delete Payment Where PaymentId in (" + PaymentIdString + ") And (PaymentState=0 or PaymentState=2)";
                    updatesql="update BuyPlanGoods set ispayment=null,paymentid=null where paymentid in("+PaymentIdString+")";
                    db.Command(updatesql);
                    db.Command(StrSql);
                    db.Close();
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "BuyPlanGoods", updatesql, "../Hospital/Payment.aspx");

                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "删除", "Payment", StrSql, "../Hospital/Payment.aspx");
                }
              

                showGridView();
            }
        }

    }

    public void showGridView()
    {
        string strConditions;
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "Payment,SupplyCompany";                                               //要查询的表明
        dbPage.Key = "PaymentId";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "distinct PaymentId,Payment.ProjectId,AccountId_YY,CompanyName_PS,UserCode_PS,PaymentName,PaymentPrice,Payment.AccountId_PS,PaymentStartDate,PaymentEndDate,Case PaymentState WHEN '0' Then '未提交' WHEN '1' Then '已确认' WHEN '2' Then '已提交' End As PaymentState,Case PaymentState  WHEN '1' Then 'disabled'  End As disabled,Case PaymentState WHEN '2' Then '修改结算单' WHEN '1' Then ''  End As UpdatePayment, PaynamtDate";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 20;                                                    //没有显示记录条数   
        dbPage.OrderBy = " PaymentId";                  //排序字段
        dbPage.OrderByIsDesc = true;
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        DlAttorney.DataSource = dr;
        DlAttorney.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键



    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp = "";
        string PaymentName, CompanyName_PS, UserCode_PS, PayStatus, PaymentId,InvoiceId;
        DateTime PaymentStartDate, PaymentEndDate;
        PaymentStartDate = Convert.ToDateTime("1900-1-1");
        PaymentEndDate = Convert.ToDateTime("1900-1-1");

        PaymentName = SysFun.ToTrim(txtPaymentName.Text);
        CompanyName_PS = SysFun.ToTrim(txtCompanyName_PS.Text);
        UserCode_PS = SysFun.ToTrim(txtUserCode_PS.Text);
        //08.13
        PaymentId = SysFun.ToTrim(txtPaymentId.Text);
        InvoiceId = SysFun.ToTrim(txtInvoiceId.Text);


        //按发票来进行查询
        //判断是否是数值。（方法不太好，小数没有去除）
        if (!InvoiceId.Equals("") )
        {
            if (SysFun.IsNumeric(InvoiceId))
            {
                strtemp += "paymentid  in  ( select distinct paymentid from  BuyPlanGoods where receiptid = '"+ InvoiceId +"') and ";
            }
            else
            {
                txtInvoiceId.Text = "";
                MsgBox.Aert("发票编号必须输入数值", "Payment.aspx");
            }
        }
        
       
        

        PayStatus = DLPayStatus.SelectedValue;
        switch (PayStatus)
        {
            case "全部":
                break;
           
            case "已提交":
                strtemp += " PaymentState=2 And";
                break;
            case "配送企业已确认":
                strtemp += "PaymentState=1 And";
                break;
        }

        // strtemp += " Payment.ProjectId=" + PayMentProjectId.ToString() + " And AccountId_YY='" + PayMentHospitalId.ToString() + "' And Payment.AccountId_PS=SupplyCompany.AccountId_PS And Payment.ProjectId=SupplyCompany.ProjectId And";
        // 2007/08/22 马
        strtemp += " AccountId_YY='" + PayMentHospitalId.ToString() + "' And Payment.AccountId_PS=SupplyCompany.AccountId_PS And";

        if (PaymentName != "")
        {
            strtemp += " PaymentName like '%" + PaymentName + "%' and";
        }
        if (CompanyName_PS != "")
        {
            strtemp += " CompanyName_PS like '%" + CompanyName_PS + "%' and";
        }
        if (UserCode_PS != "")
        {
            strtemp += " UserCode_PS like '%" + UserCode_PS + "%' and";
        }
        //08.13

        if (PaymentId != "")
        {
            strtemp += " PaymentId like '%" + PaymentId + "%' and";
        }
       
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }
        return strtemp;

    }

    #region TraDes 成员

    public string Name
    {
        get { return "结算管理"; }
    }

    public string Describe
    {
        get { return ""; }
    }

    public string ParentName
    {
        get { return "结算管理"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
            return _AccountType;
        }
    }

    #endregion

}
